{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d7c9c544",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "# range（）函数的使用\n",
    "\n",
    "## 创建range对象的三种方式\n",
    "\n",
    "| 左对齐 | 右对齐 |\n",
    "| :-----| :---- |\n",
    "| range(stop)  | 创建一个[0，stop]之间的整数序列，步长为1  |\n",
    "| range(start,stop) | 创建一个[start，stop]之间的整数序列，步长为1  |\n",
    "| range(start,stop,step) | 创建一个[start，stop]之间的整数序列，步长为step  |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "510d66a2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "range(0, 10)\n",
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
     ]
    }
   ],
   "source": [
    "nub_a = range(10)\n",
    "print(nub_a)\n",
    "print(list(nub_a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "65a7f9de",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "range(1, 10)\n",
      "[1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
     ]
    }
   ],
   "source": [
    "nub_b = range(1,10)\n",
    "print(nub_b)\n",
    "print(list(nub_b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "461dd4f4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "range(1, 200, 50)\n",
      "[1, 51, 101, 151]\n"
     ]
    }
   ],
   "source": [
    "nub_c = range(1,200,50)\n",
    "print(nub_c)\n",
    "print(list(nub_c))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "14e6be25",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# 判断整数是否在这个数列中\n",
    "print(51 in nub_c)\n",
    "\n",
    "print(10 not in nub_b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a2d0fb1",
   "metadata": {},
   "source": [
    "\n",
    "# while循环\n",
    "\n",
    "* 反复做同一件事的情况，称为循环\n",
    "* 循环结构的流程图\n",
    "* 循环的分类\n",
    "    * while\n",
    "    * for-in\n",
    "* 语法结构\n",
    "while 条件表达式\n",
    "    条件执行体（循环体）\n",
    "    \n",
    "* 选择结构的if与循环结构while的区别\n",
    "    > if是判断一次，条件为true执行一行  \n",
    "    > while是判断n+1次，条件为true执行n次\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "b5e79cde",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "a =1\n",
    "# if 只有执行一次，while为n+1次\n",
    "while a<10:\n",
    "    print(a)\n",
    "    a+=1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df94691d",
   "metadata": {},
   "source": [
    "## 计算0到4之间的累加和\n",
    "> 1. 初始化变量\n",
    "> 2. 条件判断\n",
    "> 3. 条件执行体（循环体）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "f6a52b03",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "和为 10\n"
     ]
    }
   ],
   "source": [
    "sum=0\n",
    "# 存储累加和的值\n",
    "n = 0\n",
    "# 初始化变量\n",
    "while n<=4:\n",
    "    sum+=n\n",
    "#     改变变量\n",
    "    n+=1\n",
    "print('和为',sum)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d26c927c",
   "metadata": {},
   "source": [
    "## 计算1到100的偶数和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "b8891cf9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2550\n"
     ]
    }
   ],
   "source": [
    "sum=0\n",
    "# 存储偶数和的值\n",
    "o = 1\n",
    "# 初始化变量\n",
    "# 条件判断\n",
    "while o<=100:\n",
    "#     条件执行体（求和）\n",
    "    if o%2==0:    # 加入==0算出来的是偶数和（2550），去掉的话算出来的是奇数和（2500）\n",
    "        sum+=o\n",
    "# 改变变量\n",
    "    o+=1\n",
    "print(sum)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34bbe828",
   "metadata": {},
   "source": [
    "# for-in循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "b793f425",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "p\n",
      "y\n",
      "t\n",
      "h\n",
      "o\n",
      "n\n"
     ]
    }
   ],
   "source": [
    "for item in 'python':\n",
    "    print(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "081d27e8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "# range()产生一个整数序列——》也是一个可迭代对象\n",
    "for i in range(5):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "69389e7b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "python掉发\n",
      "python掉发\n",
      "python掉发\n",
      "python掉发\n",
      "python掉发\n"
     ]
    }
   ],
   "source": [
    "# 如果在循环体中不需要使用到自定义变量，可将自定义变量变为“-”\n",
    "for _ in range(5):\n",
    "    print('python掉发')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "b5609583",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用for循环计算1到100之间的偶数和\n",
      "1到100之间的偶数和为： 2550\n"
     ]
    }
   ],
   "source": [
    "print('使用for循环计算1到100之间的偶数和')\n",
    "sum = 0\n",
    "for item in range(1,101):\n",
    "    if item%2==0:\n",
    "        sum+=item\n",
    "print('1到100之间的偶数和为：',sum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "28924408",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "计算100-1000之间的水仙花数字，什么是水仙花，比如153=1*1*1+5*5*5+3*3*3\n",
      "153 为水仙花数\n",
      "370 为水仙花数\n",
      "371 为水仙花数\n",
      "407 为水仙花数\n"
     ]
    }
   ],
   "source": [
    "print('计算100-1000之间的水仙花数字，什么是水仙花，比如153=1*1*1+5*5*5+3*3*3')\n",
    "for item in range(100,1000):\n",
    "    ge = item%10\n",
    "    shi = item//10%10   #整除\n",
    "    bai = item//100\n",
    "    # print(bai,shi,ge)得出由101到999 的全部数字\n",
    "    if ge**3+shi**3+bai**3==item:\n",
    "        print(item,'为水仙花数')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "01f417a7",
   "metadata": {},
   "source": [
    "# break、continue与else语句\n",
    "\n",
    "# 嵌套循环"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d0639cd0",
   "metadata": {},
   "source": [
    "# 嵌套循环"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
